package com.fingersoft.feature.personal


import android.app.Activity
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import cn.fingersoft.feature.personal.SealPersonalContextBase
import com.fingersoft.common.ICallback
import com.fingersoft.feature.newlock.LockManager
import com.fingersoft.feature.newlock.LockManager.Companion.LOCK_TYPE_GESTURE
import com.fingersoft.feature.newlock.LockManager.Companion.LOCK_TYPE_PIN
import com.fingersoft.feature.personal.api.UpdateHeaderData
import com.fingersoft.feature.personal.api.UpdateHeaderResponse2
import com.fingersoft.im.api.UserAgent
import com.fingersoft.im.api.base.BaseModelCallback2
import com.fingersoft.im.utils.AppUtils
import com.fingersoft.im.utils.JSONUtils
import com.fingersoft.im.view.LoadDialog
import com.lzy.okgo.OkGo
import com.lzy.okgo.request.BaseRequest
import okhttp3.Call
import okhttp3.Response
import java.io.File
import java.net.URI
import java.net.URISyntaxException


internal abstract class SealPersonalContext2 : SealPersonalContextBase() {

    override fun resetPin() {
        LockManager.instance.doSetOrResetPassword(LOCK_TYPE_PIN)
    }

    override fun isUsePinLock(): Boolean {
        return LockManager.instance.isUseLock(LOCK_TYPE_PIN)
    }

    override fun resetGesturePassword() {
        LockManager.instance.doSetOrResetPassword(LOCK_TYPE_GESTURE)
    }



    override fun disableAllLock() {
        LockManager.instance.disableAllLock()
    }

    override fun isAllLockDisabled(): Boolean {
        return LockManager.instance.isAllLockDisabled()
    }

    override fun isUseCloseAllLock(): Boolean {
        return LockManager.instance.isUseCloseAllLock()
    }

    override fun useGestureLock(): Boolean {
        return LockManager.instance.isUseLock(LOCK_TYPE_GESTURE)
    }

    override fun hasPinPassword(): Boolean {
        return LockManager.instance.hasPassword(LOCK_TYPE_PIN)
    }

    override fun getGesturePassWord(): String? {
        return LockManager.instance.getPassword(LOCK_TYPE_GESTURE)
    }

    override fun removeGesture() {
        LockManager.instance.setPassword(LOCK_TYPE_GESTURE, null)
    }

    override fun setGesturePassword(password: String?) {
        LockManager.instance.setPassword(LOCK_TYPE_GESTURE, password)
    }

    override fun isSaveGesture(): Boolean? {
        return LockManager.instance.isSetLock(LOCK_TYPE_GESTURE)
    }

    override fun changerGesturePassword(): Boolean? {
        return LockManager.instance.hasPassword(LOCK_TYPE_GESTURE)
    }

    override fun reSetGesture() {
        LockManager.instance.doAuthenticationReSet(LOCK_TYPE_GESTURE)
    }

    override fun setLockGestureEnable(value: Boolean) {
        LockManager.instance.setEnable(LOCK_TYPE_GESTURE, value)
    }

    override fun showLockChooseTypeDialog(activity: AppCompatActivity) {
        LockManager.instance.doChooseType(activity)
    }

    override fun updateHeader(context: Activity?, path: String?, uri: Uri?, callback: ICallback<UpdateHeaderData>?) {
        try {
            OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(UserAgent(context, AppUtils.getTokenInfo().userToken, AppUtils.getTokenInfo().did)))
                .params("icon", File(URI(uri.toString())))
                .execute<UpdateHeaderResponse2>(object : BaseModelCallback2<UpdateHeaderResponse2>(UpdateHeaderResponse2::class.java) {
                    override fun onSuccess(updateHearResponse2: UpdateHeaderResponse2, call: Call?, response: Response?) {
                        super.onSuccess(updateHearResponse2, call, response)
                        if (AppUtils.showApiSucceedErrorToast(updateHearResponse2)) {
                            callback?.onError()
                            return
                        }
                        callback?.onSuccess(updateHearResponse2.data)
                    }

                    override fun onError(call: Call?, response: Response?, e: Exception?) {
                        super.onError(call, response, e)
                        callback?.onError()
                    }

                    override fun onBefore(request: BaseRequest<*>?) {
                        super.onBefore(request)
                        LoadDialog.show(context)
                    }

                    override fun onAfter(updateHeaderResponse2: UpdateHeaderResponse2?, e: Exception?) {
                        super.onAfter(updateHeaderResponse2, e)
                        LoadDialog.dismiss(context)
                    }
                })
        } catch (e: URISyntaxException) {
            e.printStackTrace()
            callback?.onError()
        }

    }
}
